<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：顾问列表
 *
 **/

namespace Qwadmin\Controller;
use Qwadmin\Model\AdviserModel;

class AboutController extends ComController
{
    public function index() {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $where['deleted'] = AdviserModel::STATUS_ON;
        $order = 'id desc';
        if (!empty($_GET['name'])) {
            $where['name'] = ['like','%'.$_GET['name'].'%'];
        }

        if (!empty($_GET['order'])) {
            $order = 'id '.$_GET['order'];
        }
        $adviser = M('adviser');
        $pagesize = 10;//每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $adviser->where($where)->count();

        $list = $adviser->order($order)->where($where)->limit($offset . ',' . $pagesize)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

        foreach ($list as $k=> &$v) {
            $v['dk_type'] = AdviserModel::DK_TYPE[$v['dk_type']];
            $v['dk_edu']  = AdviserModel::DK_EDU[$v['dk_edu']];
            $v['dk_month_lilv'] = AdviserModel::DK_MONTH_LILV[$v['dk_month_lilv']];
        }
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function add() {

        $areas = M("areas");
        $provinces = $areas->where("deleted=0 AND parent_id=1")->select();
        $this->assign('provinces', $provinces);
        if(!empty($provinces['area_id'])){
            // 获取所在省下面的所有市
            $cities = $areas->where("deleted=0 AND parent_id={$provinces['area_id']}")->select();
            $this->assign('cities', $cities);
        }
        if(!empty($cities['area_id'])){
            // 获取市下面的所有区
            $district = $areas->where("deleted=0 AND parent_id={$cities['area_id']}")->select();
            $this->assign('district', $district);
        }

        $this->assign('dk_type',AdviserModel::DK_TYPE);
        $this->assign('dk_edu',AdviserModel::DK_EDU);
        $this->assign('dk_month_lilv',AdviserModel::DK_MONTH_LILV);
        $this->display('form');
    }

    public function edit() {

        $uid = isset($_GET['id']) ? intval($_GET['id']) : false;
        if (empty($uid)) {
           $this->error('参数错误');
        }

        $data = [];
        $data = M('adviser')->where(['id'=> $uid])->find();
        if(!empty($data)){
            $areas = M("areas");
            $province_id = $data['service_province'];
            $city_id = $data['service_city'];
            // 获取所有省
            $provinces = $areas->where("deleted=0 AND parent_id=1")->select();
            $this->assign('provinces', $provinces);
            if(!empty($province_id)){
                // 获取所在省下面的所有市
                $cities = $areas->where("deleted=0 AND parent_id=$province_id")->select();
                $this->assign('cities', $cities);
            }
            if(!empty($city_id)){
                // 获取市下面的所有区
                $district = $areas->where("deleted=0 AND parent_id=$city_id")->select();
                $this->assign('district', $district);
            }
        }
        $this->assign('dk_type',AdviserModel::DK_TYPE);
        $this->assign('dk_edu',AdviserModel::DK_EDU);
        $this->assign('dk_month_lilv',AdviserModel::DK_MONTH_LILV);
        $this->assign('member',$data);
        $this->display('form');
    }

    public function update() {

        $data = [
            'id'               => $_POST['id'],
            'name'             => $_POST['name'],
            'headurl'          => $_POST['headurl'],
            'dk_type'          => $_POST['dk_type'],
            'dk_edu'           => $_POST['dk_edu'],
            'dk_month_lilv'    => $_POST['dk_month_lilv'],
            'dk_desc'          => $_POST['dk_desc'],
            'qrcode'           => $_POST['qrcode'],
            'service_province' => $_POST['province_id'],
            'service_city'     => $_POST['city_id'],
            'address'          => $_POST['address'],
        ];

        $model = M('adviser');
        if (empty($data['id'])) { //增加
            $data['ctime']= date('Y-m-d H:i:s');
            if ($model->add($data)) {
               $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        } else { //编辑
            if ($model->save($data)) {
                $this->success('编辑成功');
            } else {
                $this->error('编辑失败');
            }
        }

    }

    public function del() {

        $uids = isset($_REQUEST['uids']) ? $_REQUEST['uids'] : false;
        $map['id'] = array('in', $uids);
        if (M('adviser')->where($map)->delete()) {
            addlog('删除顾问UID：' . $uids);
            $this->success('顾问删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

}